// components/operate/operate.js
// import api from '../../utils/request.js'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    Data: {
      type: Object,
      value: {}
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    audiolist: {},
    // 菜单动画
    animations: "",
    // 收藏动画
    animations1: "",
    // 我的歌单
    meSongList: [],
    likeMusics: 0,
    host: 'http://192.168.0.26:8081',
    hidden: true,
    // 输入内容
    value: ""
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 输入框内容改变事件
    inputChange(e) {
      this.setData({
        value: e.detail.value
      })
    },

    // 屏蔽
    shield() {
      this.animation.bottom(-500).step() //对动画进行简单的描述
      this.setData({
        animation1: this.animation.export(),
        menuMask: false,
        menuFlog: true
      })
    },

    // 屏蔽遮罩层事件
    shields() {
      this.setData({
        menuFlog: false
      })
    },

    // 屏蔽歌手或歌曲
    screen(e) {
      var id = e.currentTarget.dataset.id;
    },

    // 黑名单
    blacklist() {
    },

    // 收藏
    collect() {
      let token = wx.getStorageSync("token");
      if (token == 1) {
        this.animation.bottom(-500).step() //对动画进行简单的描述
        this.setData({
          animations: this.animation.export()
        })
        this.animation.bottom(0).step() //对动画进行简单的描述
        this.setData({
          animations1: this.animation.export()
        })
      } else {
        wx.showModal({
          title: "请先登录",
          cancelColor: 'cancelColor',
        })
      }
    },

    // 菜单
    menu() {
      this.animation.bottom(0).step() //对动画进行简单的描述
      this.setData({
        animations: this.animation.export()
      })
      this.animation.bottom(-500).step() //对动画进行简单的描述
      this.setData({
        animations1: this.animation.export()
      })
    },

    // 获取喜爱的音乐
    getLike() {
      let userCode = wx.getStorageSync('userCode');
      wx.request({
        url: `${this.data.host}/userLike/getUserLikeByUserCode`,
        method: "post",
        data: {
          userCode: userCode
        },
        success: (res) => {
          this.setData({
            likeMusics: res.data.data.length
          })
        }
      })
    },

    // 获取用户歌单
    getSongList() {
      let userid = wx.getStorageSync('userid');
      wx.request({
        url: `${this.data.host}/songSheet/getSongSheetByUserId?id=${userid}`,
        method: "get",
        success: (res) => {
          let list = res.data.data;
          let arr = [];
          for (let i = 0; i < list.length; i++) {
            arr[i] = {};
            if (list[i].imgUrl != '' && list[i].imgUrl != null) {
              arr[i].imgUrl = this.data.host + list[i].imgUrl;
            } else {
              arr[i].imgUrl = '';
            }
            arr[i].songsheetName = list[i].songsheetName;
            arr[i].includeSinger = list[i].includeSinger;
            arr[i].id = list[i].id;
          }
          this.setData({
            meSongList: arr
          })
        }
      })
    },

    // 添加到我喜欢
    likes() {
      let userCode = wx.getStorageSync('userCode');
      let musicCode = this.data.audiolist.musicCode;
      wx.request({
        url: this.data.host + "/userLike/addUserLike",
        method: "post",
        data: {
          userCode: userCode,
          musicCode: musicCode
        },
        success: (res) => {
          if (res.data.msg == "操作成功") {
            wx.showModal({
              title: "添加成功",
              cancelColor: 'cancelColor',
            })
            this.getLike();
          } else {
            wx.showModal({
              title: "添加失败",
              cancelColor: 'cancelColor',
            })
          }
          // this.data.likeMusics = res.data.data.length;
        }
      })
    },

    // 添加到我的歌单
    meSong(e) {
      let songSheetId = e.currentTarget.dataset.id;
      let musicId = this.data.audiolist.id;
      wx.request({
        url: this.data.host + `/songSheet/addMusicToSongSheet?musicId=${musicId}&songSheetId=${songSheetId}`,
        method: "post",
        success: (res) => {
          if (res.data.msg == "操作成功") {
            wx.showModal({
              title: "添加成功",
              cancelColor: 'cancelColor',
            })
            // 获取用户歌单
            this.getSongList();
          } else {
            wx.showModal({
              title: "添加失败",
              cancelColor: 'cancelColor',
            })
          }
        }
      })

    },

    // 取消
    cancel: function () {
      this.setData({
        hidden: true
      });
    },

    // 确认
    confirm: function () {
      let userid = wx.getStorageSync('userid');
      let name = this.data.value;
      let myDate = new Date();
      let time = `${myDate.getFullYear()}-${myDate.getMonth() + 1}-${myDate.getDate()}`
      this.setData({
        hidden: true
      });

      if (name.trim() != '') {
        // 添加歌单
        wx.request({
          url: `${this.data.host}/songSheet/addSongSheet`,
          method: "post",
          data: {
            songsheetName: name,
            releaseTime: time,
            userId: userid
          },
          success: (res) => {
            if (res.data.msg == "操作成功") {
              wx.showModal({
                title: "添加成功",
                cancelColor: 'cancelColor',
              })
              this.getSongList();
            }
          }
        })
      } else {
        wx.showModal({
          title: "内容不能为空",
          cancelColor: 'cancelColor',
        })
      }
    },

    // 弹出新建歌单输入框
    NewSongList() {
      this.setData({
        hidden: false
      })
    },

    // 获取父组件传的值
    getDate() {
      this.setData({
        audiolist: {
          songName: this.data.Data.songName,
          artist: !this.data.Data.artist ? "暂无" : this.data.Data.artist,
          album: this.data.Data.album,
          id: this.data.Data.id,
          musicCode: this.data.Data.musicCode
        }
      })
    },

  },
  attached() {
    // 动画
    this.animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'linear',
      delay: 100,
      transformOrigin: "left top 0"
    })

    // 获取喜爱的音乐
    this.getLike();

    // 获取用户歌单
    this.getSongList();

  },
  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show: function () {

      // 获取父组件传的值
      this.getDate();
    },
    hide: function () {
    },
    resize: function () {
    },
  },
})
